// import axios from 'axios'

const _upload = async (file) => {
    if (file === undefined) return;
    const configs = await getOSSConfig();
    const postParams = {
        key: configs.dir + '$' + '{filename}',
        policy: configs.policy,
        OSSAccessKeyId: configs.accessid,
        callback: configs.callback,
        // 让服务端返回200,不然，默认会返回204
        success_action_status: '200',
        signature: configs.signature
    }
    const fd = new FormData()
    for (const key in postParams) {
        if (postParams.hasOwnProperty(key)) {
            fd.append(key, postParams[key])
        }
    }
    fd.append('file', file, file.name)
    return axios.post(configs.host, fd).then(({ data }) => data.data)
}
const getOSSConfig = async () => {
    axios.defaults.withCredentials = false
    return await new Promise((reslove, reject) => {
        axios("http://mdg.vkforest.com/interface-formal/public/index.php/index/oss/ossConfig").then(res => {
            reslove(res.data.data)
        })
    });
}

export default _upload